import { createApp } from 'vue'
import 'element-plus/dist/index.css'
import App from './App.vue'
import router from './router'
import store from './store'
import ui from './ui-factory';
import {getDevicePage} from '@/lib/device';
import { register } from '@yanxiu/directive-link';
import * as ElementPlusIconsVue from '@element-plus/icons-vue';
import { getQueryString } from '@/lib/tools';
import { getTokenByTicket } from '@/global/container/app-context';
import createContainer from '@/global/app-container';
const view = createContainer('app')(App);

var app = createApp(view);
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
register(app);
// app.use(ui);

// 路由检测符合跳转条件则转入对应手机端路由
// router.beforeEach((to, from) => {
//   if (to.name) {
//       getDevicePage({
//           routeName: to.name as string,
//           queries: to.query,
//           params: to.params,
//           mobile: 'appmooc',
//           web: 'mooc',
//           curDevice: 'web'
//       });
//   }
// });

// 注册ui库组件
if (process.env.VUE_APP_SSO_Login_URL) {
    // 通过公共登录平台（cas.yanxiu.com）登录，会生成ticket,项目中用query中的ticket替换成token种下
    getTokenByTicket((res) => {
        store.dispatch('loginSuccess', {
            'access_token': res.data,
            keepCookie: getQueryString(window.location.href, 'keepCookie') ? true : false
        })
    }).finally(() => {
        ui.register(app).use(store).use(router).mount('#app');
    });
} else {
    ui.register(app).use(store).use(router).mount('#app');
}
